using System;
using System.Collections.Generic;
using Jalal.Library.DataStructure;
using BCGC.BLL.Helper;
using BCGC.DAL;
using System.Data;
using Jalal.Library.Data;

namespace BCGC.BLL.MarketPlace
{

    public class Market
    {

        private int itemID;
        public int ItemID
        {
            get
            {
                return itemID;
            }
        }

        private int userID;
        public int UserID
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        private String userName;
        public String UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
            }
        }

        private String userAvater;
        public String UserAvater
        {
            get
            {
                return userAvater;
            }
            set
            {
                userAvater = value;
            }
        }

        private String userMode;
        public String UserMode
        {
            get
            {
                return userMode;
            }
            set
            {
                userMode = value;
            }
        }

        private String title;
        public String Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        private String marketContent;
        public String MarketContent
        {
            get
            {
                return marketContent;
            }
            set
            {
                marketContent = value;
            }
        }

        private DateTime date;
        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
            }
        }

        private bool showContactInfo;
        public bool ShowContactInfo
        {
            get
            {
                return showContactInfo;
            }
            set
            {
                showContactInfo = value;
            }
        }

        private bool isActive;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                isActive = value;
            }
        }

        private String adminComment;
        public String AdminComment
        {
            get
            {
                return adminComment;
            }
            set
            {
                adminComment = value;
            }
        }

        public Market(int userID, String userName, String userAvater, String userMode, String title, String marketContent, DateTime date, bool showContactInfo, bool isActive, string adminComment)
        {
            UserID = userID;
            UserName = userName;
            UserAvater = userAvater;
            UserMode = userMode;
            Title = title;
            MarketContent = marketContent;
            Date = date;
            ShowContactInfo = showContactInfo;
            IsActive = isActive;
            AdminComment = adminComment;
        }

        public Market(DataRow dataRow)
        {
            if (dataRow != null)
            {

                itemID = (dataRow["ItemID"] == System.DBNull.Value ? 0 : (Int32)dataRow["ItemID"]);
                userID = (dataRow["UserID"] == System.DBNull.Value ? 0 : (Int32)dataRow["UserID"]);
                userName = (dataRow["UserName"]== System.DBNull.Value ? string.Empty :(String)dataRow["UserName"]);
                userAvater = (dataRow["UserAvater"] == System.DBNull.Value ? string.Empty : (String)dataRow["UserAvater"]);
                userMode = (dataRow["UserMode"] == System.DBNull.Value ? string.Empty : (String)dataRow["UserMode"]);
                title = (dataRow["Title"] == System.DBNull.Value ? string.Empty : (String)dataRow["Title"]);
                marketContent = (dataRow["MarketContent"] == System.DBNull.Value ? string.Empty : (String)dataRow["MarketContent"]);
                date = (dataRow["Date"] == System.DBNull.Value ? DateTime.MinValue: (DateTime)dataRow["Date"]);
                showContactInfo = (dataRow["ShowContactInfo"] == System.DBNull.Value ? false : (Boolean)dataRow["ShowContactInfo"]);
                isActive = (dataRow["IsActive"] == System.DBNull.Value ? false : (Boolean)dataRow["IsActive"]);
                adminComment = (dataRow["AdminComment"] == System.DBNull.Value ? string.Empty : (String)dataRow["AdminComment"]);

            }
            
        }
        
        private static ReadWriteCustomCollection<Market> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<Market> collection = new ReadWriteCustomCollection<Market>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new Market(dr));
            }
            return collection;
        }
        public static ReadWriteCustomCollection<Market> GetAllMarketListing()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = MarketPlaceData.GetAllMarketListing();
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }

        public static Market GetByID(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = MarketPlaceData.GetMarketByID(id);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new Market(result.Rows[0]);
            else
                return null;
        }

        public bool Insert(Market market)
        {
            throw new System.Exception("Not implemented");
        }

        public bool Update()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result=MarketPlaceData.UpdateMarket(this.ItemID,this.IsActive,this.AdminComment,this.MarketContent,this.ShowContactInfo);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;

        }

        
        //public bool Delete(int id, Admin admin)
        //{
        //    throw new System.Exception("Not implemented");
        //}

    }

}
